package com.arpa.oms.controller;


import cn.hutool.core.bean.BeanUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.entity.Shipment;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.service.IShipmentService;
import com.arpa.oms.domain.dto.OmsWarehouseDTO;
import com.arpa.oms.domain.vo.OmsWarehouseVO;
import com.arpa.oms.service.IOmsWarehouseService;
import com.arpa.wms.domain.dto.WarehouseDTO;
import com.arpa.wms.domain.vo.WarehouseVO;
import com.arpa.wms.service.IWarehouseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * OMS仓库 Controller
 * </p>
 *
 * @author wangdong
 * @since 2021-07-21
 */
@Api(tags = "OMS仓库管理")
@RestController
@RequestMapping(value = "/omsWarehouse", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsWarehouseController {

    private final IOmsWarehouseService iOmsWarehouseService;
    @Autowired
    private IShipmentService iShipmentService;

    public OmsWarehouseController(IOmsWarehouseService iOmsWarehouseService) {
        this.iOmsWarehouseService = iOmsWarehouseService;
    }

    /**
     * 获取 仓库 列表
     *
     * @param omsWarehouseDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 仓库 列表", notes = "获取 仓库 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer ")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(OmsWarehouseDTO omsWarehouseDTO, HttpServletRequest request) {
        try {

            //通过登录选择的仓库过滤数据
            omsWarehouseDTO.setGroupCode(UserUtil.getBranchCode());
            omsWarehouseDTO.setWarehouseCodes(UserUtil.getShipmentAuthorizeWarehouseCodes());
            OmsWarehouseVO warehouseVOSum = iOmsWarehouseService.queryListSum(omsWarehouseDTO);
            List<OmsWarehouseVO> warehouseVOList = iOmsWarehouseService.queryList(omsWarehouseDTO);
            Page returnPage = new Page(omsWarehouseDTO.getPageNum(), omsWarehouseDTO.getPageSize());
            returnPage.setRecords(warehouseVOList);
            returnPage.setTotal(warehouseVOSum.getTotal());
            returnPage.setSumObject(warehouseVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }



    /**
     * WMS业务操作获取仓库数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "OMS获取登录时选择的仓库标识或入驻商标识", notes = "仅限WMS业务操作页面下拉框使用。比如：" +
            "入库管理、出库管理中的列表、表单等页面使用，返回选择的仓库授权的入驻商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer ")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping("/business/shipments")
    public Result businessChooseData(
            HttpServletRequest request) {
        try {
            String shipmentCodes =UserUtil.getLoginWarehouseCodeOrShipmentCode();
            assert shipmentCodes != null;
            List<Shipment> shipmentList = iShipmentService.list(new LambdaQueryWrapper<Shipment>()
                    .in(Shipment::getPartyCode,shipmentCodes)
            );
            List<Map<String, Object>> shipments = shipmentList.stream().map(BeanUtil::beanToMap).collect(Collectors.toList());
            return Result.ok().data(shipments);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }
}
